package com.df.springmvc.workflow.trace.controller;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;
import com.common.bean.DhtmlGridBean;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.SupperController;
import com.df.springmvc.workflow.trace.services.TraceManageServices;

@Controller
@RequestMapping("/workflow/trace/TraceManage")
/**
 * 本类仅做一些简单数据处理如:
 *格式验证
 *数据封装
 * @author Turner
 *
 */
public class TraceManageController extends SupperController{
	String page_trace="workflow/trace/" + this.getClass().getSimpleName().replace("Controller","") + "_";
	@Autowired
	TraceManageServices services;
	@Autowired
	MessageSource messageSource;
	
	public TraceManageController(){
		// TODO Auto-generated constructor stub
	}
	@RequestMapping(value={"/"})
	public String excute(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		return page_trace;
	}
	@RequestMapping(value={"/query"})
	public void query(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			DhtmlGridBean gridBean=services.query(getParam_PagingBean(request),getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(gridBean));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@RequestMapping(value={"/portlet"})
	public void portlet(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			DhtmlGridBean gridBean=services.portlet(getParam_PagingBean(request),getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(gridBean));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 进行流程处理页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value={"/queryTaskInfo"})
	public void queryTaskInfo(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			HashMap<String,String> paramMap=getParamMap(request,"map");
			WebUtil.json(response,new MsgBean(services.queryFromUrl(paramMap),true));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
	@RequestMapping(value={"/" + toDel})
	public String toDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		return page_trace + Del;
	}
	@Override
	@RequestMapping("/toView")
	public String toView(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			String ExecutionID=getParam(request,"ExecutionID");
			String processInstanceId=getParam(request,"processInstanceId");
			Object rs=services.queryList(ExecutionID,processInstanceId);
			addRequestAttribute("list",rs);
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
		}finally{
		}
		return page_trace + View;
	}
	@RequestMapping(value={"/" + doDel})
	public void doDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// WebUtil.json(response,new MsgBean("OK"));
		try{
			Object rs=services.del(getParamMap(request,"map"));
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
	@RequestMapping("/" + "toPortlet")
	public String toPortlet(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		return page_trace + "Portlet";
	}
	@RequestMapping("/" + "getPicPreview")
	public void getPicPreview(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			File is=services.getPicPreview(request.getParameter("path"),WebUtils.getRealPath(request.getServletContext(),"/"));
			WebUtil.json(response,new MsgBean(is.getAbsolutePath()));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
	@RequestMapping("/tracePic/{processInstanceId}" + "/{ExecutionID}")
	public void tracePic(ModelMap model,HttpServletRequest request,HttpServletResponse response,@PathVariable String ExecutionID,@PathVariable String processInstanceId){
		try{
			InputStream is=services.tracePic(ExecutionID,processInstanceId);
			byte[] b=new byte[1024];
			int len=-1;
			while((len=is.read(b,0,1024)) != -1){
				response.getOutputStream().write(b,0,len);
			}
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}
	}
}
